/**
 * 删除上传的图片
 */
function deleteCover(value) {
	$("#" + value).remove();
}

/**
 * 主页
 */
define(['jquery',
	'base',
	'baseUtils',
    'msgTool',
	'css!global/plugins/fileInput/fileinput.min.css',
	'plugins/fileInput/fileinput.min',
	'plugins/address/distpicker',
	'plugins/address/distpicker.data'
	], function ($, base, baseUtils,msgTool, _, _, _) {
	//默认加载基础库
	base();
	//执行
	return {
		info: function () {
			//类型
			var   companyLicense = "《营业执照》照片，可上传多张。";
			var   rentalContract= "《房屋租赁合同》照片，可上传多张";
			var   layout="《布局图》，可上传多张";
			var   operatingProcedures="《操作流程》照片，可上传多张";
			var   orientationMap="《方位图》照片，可上传多张";
			//营业执照
			doUploader("#companyLicenseUploader", "请选择" + companyLicense, "#companyLicense-upload-error", "#companyLicense-upload-success", "companyLicenseList");
			//房屋租赁合同
			doUploader("#rentalContractUploader", "请选择" + rentalContract, "#rentalContract-upload-error", "#rentalContract-upload-success", "rentalContractList");
			//布局图
			doUploader("#layoutUploader", "请选择" + layout, "#layout-upload-error", "#layout-upload-success", "layoutList");
			//操作流程
			doUploader("#operatingProceduresUploader", "请选择" + operatingProcedures, "#operatingProcedures-upload-error", "#operatingProcedures-upload-success", "operatingProceduresList");
			//方位图
			doUploader("#orientationMapUploader", "请选择" + orientationMap, "#orientationMap-upload-error", "#orientationMap-upload-success", "orientationMapList");


			function doUploader(node, placeholder, elErrorContainer, successContainer, listName) {
				return $(node).fileinput({
					//设置语言
					language: 'zh',
					//上传的地址
					uploadUrl: '/common/uploader/index',
					method: 'post',
					msgPlaceholder: placeholder,
					//附加属性(0图片，1文件)
					uploadExtraData: {filesType: 0},
					//上传类型
					allowedFileExtensions: ['jpg', 'gif', 'png', 'jpeg'],
					//是否显示图片展示框
					showPreview: false,
					//是否显示上传按钮
					showUpload: true,
					showCancel: false,
					//是否显示上传输入框
					showCaption: true,
					//是否显示删除键
					showRemove: true,
					//浏览图标
					browseIcon: '<i class="iconfont icon-wenjianjia"></i>&nbsp;',
					//浏览样式
					browseClass: 'btn btn-success btn-outline',
					//上传图标
					uploadIcon: '<i class="iconfont icon-upload"></i>&nbsp;',
					//上传样式
					uploadClass: 'btn btn-success btn-outline',
					//移除图标
					removeIcon: '<i class="iconfont icon-remove"></i>&nbsp;',
					//移除样式
					removeClass: 'btn btn-info btn-outline',
					//单位为kb，(30M)如果为0表示不限制文件大小
					maxFileSize: 10720,
					//错误显示位置
					elErrorContainer: elErrorContainer,
					//错误显示位置节点样式
					msgErrorClass: 'alert alert-block alert-danger',
					//验证数量
					validateInitialCount: true
				}).on("fileuploaded", function (event, data) {
					if (data.response !== null && data.response.status) {
						$(successContainer).append("<div class=\"col-xs-12 col-sm-6 col-md-3 mb10\" id=\"" + data.response.nodeId + "\"><div class=\"thumbnail uploadImageShow\"><input type=\"hidden\" name=\"" + listName + "\" value=\"" + data.response.url + "\"><a href=\"" + data.response.domain + data.response.url + "\" target='_blank'><img src=\"" + data.response.domain + data.response.url + "\"></a><div class=\"btn btn-block btn-danger deletetCover\" onclick=\"deleteCover('" + data.response.nodeId + "')\">删除</div></div></div>");
					} else {
						$(elErrorContainer).text(data.response.message);
					}
				});
			}


			/**
			 * 提交食药申请材料
			 */

			$("#submitBase").on('click', function () {
				var companyName = $("#companyName").val();
				var companyLicense= $("input[name='companyLicenseList']").val();
				var rentalContract= $("input[name='rentalContractList']").val();
				var layout= $("input[name='layoutList']").val();
				var operatingProcedures= $("input[name='operatingProceduresList']").val();
				var orientationMap= $("input[name='orientationMapList']").val();
				if(companyName==''){
					$("#companyName-error").html("(公司名称不能为空)");
					return false;
				}else{
					$("#companyName-error").html("");
				}
				if(companyLicense==undefined){
					$("#companyLicense-error").html("(营业执照不能为空)");
					return false;
				}else{
					$("#companyLicense-error").html("");
				}
				if(rentalContract==undefined){
					$("#rentalContract-error").html("(租赁合同不能为空)"); 
					return false;
				}else{
					$("#rentalContract-error").html(""); 
				}
				if(layout==undefined){
					$("#layout-error").html("(布局图不能为空)"); 
					return false;
				}else{
					$("#layout-error").html("");  
				}
				if(operatingProcedures==undefined){
					$("#operatingProcedures-error").html("(操作流程图不能为空)");
					return false;
				}else{
					$("#operatingProcedures-error").html(""); 
				}
				if(orientationMap==undefined){
					$("#orientationMap-error").html("(方位图不能为空)"); 
					return false;
				}else{
					$("#orientationMap-error").html(""); 
				}
				$("#saveBaseForm").submit();
			});

		},

		save: function () {
	
			$("#submitApply").on('click', function () {
				 $.ajax({
                   url: "/fooddrugCancel/saveApply",
                   method: "POST",
                   dataType: "json",
                   data:$('#saveCancelForm').serialize(),
                   success: function (data) {
                        window.location.href="/user/transact";
                        msgTool.success("提交成功！","信息提示");
                   },
                   error: function (jqXHR) {
                       msgTool.error(jqXHR.status + " (" + jqXHR.statusText + ")", "错误提示");
                       return false;
                   }
               })
			});
			
		      
		}
	}

});
